package windows;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import data.Scoala;

public class ModifySchoolFrame {

	private JFrame frame;
	private JTextField textFieldSchoolName = new JTextField();
	private JTextField textFieldSchoolStreet = new JTextField();
	private JTextField textFieldSchoolNumber = new JTextField();
	private JTextField textFieldSchoolTown = new JTextField();
	private JTextField textFieldSchoolDirector = new JTextField();
	private JTextField textFieldSchoolContabil = new JTextField();
	private JButton modifySchoolButton = new JButton("Save");
	private JButton cancelSchoolButton = new JButton("Cancel");

	public JButton getModifySchoolButton() {
		return modifySchoolButton;
	}

	public JButton getCancelSchoolButton() {
		return cancelSchoolButton;
	}

	public JFrame getFrame() {
		return frame;
	}

	public JTextField getTextFieldSchoolContabil() {
		return textFieldSchoolContabil;
	}

	public JTextField getTextFieldSchoolDirector() {
		return textFieldSchoolDirector;
	}

	public JTextField getTextFieldSchoolName() {
		return textFieldSchoolName;
	}

	public JTextField getTextFieldSchoolNumber() {
		return textFieldSchoolNumber;
	}

	public JTextField getTextFieldSchoolStreet() {
		return textFieldSchoolStreet;
	}

	public JTextField getTextFieldSchoolTown() {
		return textFieldSchoolTown;
	}

	public void setAddSchoolButton(JButton addSchoolButton) {
		this.modifySchoolButton = addSchoolButton;
	}

	public void setCancelSchoolButton(JButton cancelSchoolButton) {
		this.cancelSchoolButton = cancelSchoolButton;
	}

	public void setFrame(Scoala school) {
		frame = new JFrame("Modifica scoala");
		JPanel panel = new JPanel();
		SpringLayout layout = new SpringLayout();

		JLabel labelSchoolName = new JLabel("Nume scoala");
		JLabel labelFieldSchoolStreet = new JLabel("Strada");
		JLabel labelFieldSchoolNumber = new JLabel("Numar");
		JLabel labelFieldSchoolTown = new JLabel("Oras");
		JLabel labelFieldSchoolDirector = new JLabel("Director");
		JLabel labelFieldSchoolContabil = new JLabel("Contabil");

		textFieldSchoolName.setPreferredSize(new Dimension(300, 30));
		textFieldSchoolStreet.setPreferredSize(new Dimension(300, 30));
		textFieldSchoolNumber.setPreferredSize(new Dimension(300, 30));
		textFieldSchoolTown.setPreferredSize(new Dimension(300, 30));
		textFieldSchoolDirector.setPreferredSize(new Dimension(300, 30));
		textFieldSchoolContabil.setPreferredSize(new Dimension(300, 30));

		if (isText(school.getNumeScoala()))
			textFieldSchoolName.setText(school.getNumeScoala());
		else
			textFieldSchoolName.setText(null);
		 textFieldSchoolName.requestFocusInWindow();
		 textFieldSchoolName.selectAll();
		 
		if (isText(school.getSchoolStreet()))
			textFieldSchoolStreet.setText(school.getSchoolStreet());
		else
			textFieldSchoolStreet.setText(null);

		if (school.getSchoolNumber() != -5)
			textFieldSchoolNumber.setText(school.getSchoolNumber().toString());
		else
			textFieldSchoolNumber.setText(null);

		if (isText(school.getSchoolTown()))
			textFieldSchoolTown.setText(school.getSchoolTown());
		else
			textFieldSchoolTown.setText(null);

		if (isText(school.getSchoolPrincipal()))
			textFieldSchoolDirector.setText(school.getSchoolPrincipal());
		else
			textFieldSchoolDirector.setText(null);

		if (isText(school.getSchoolAccountant()))
			textFieldSchoolContabil.setText(school.getSchoolAccountant());
		else
			textFieldSchoolContabil.setText(null);

		modifySchoolButton.setActionCommand("modifySchoolCommand");
		cancelSchoolButton.setActionCommand("cancelModifySchoolCommand");

		panel.add(labelSchoolName);
		panel.add(textFieldSchoolName);
		panel.add(labelFieldSchoolStreet);
		panel.add(textFieldSchoolStreet);
		panel.add(labelFieldSchoolNumber);
		panel.add(textFieldSchoolNumber);
		panel.add(labelFieldSchoolTown);
		panel.add(textFieldSchoolTown);
		panel.add(labelFieldSchoolDirector);
		panel.add(textFieldSchoolDirector);
		panel.add(labelFieldSchoolContabil);
		panel.add(textFieldSchoolContabil);

		panel.add(modifySchoolButton);
		panel.add(cancelSchoolButton);

		panel.setLayout(layout);

		layout.putConstraint(SpringLayout.WEST, textFieldSchoolName, 100,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, textFieldSchoolName, 30,
				SpringLayout.NORTH, panel);

		layout.putConstraint(SpringLayout.EAST, labelSchoolName, -5,
				SpringLayout.WEST, textFieldSchoolName);
		layout.putConstraint(SpringLayout.NORTH, labelSchoolName, 35,
				SpringLayout.NORTH, panel);

		layout.putConstraint(SpringLayout.WEST, textFieldSchoolStreet, 100,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, textFieldSchoolStreet, 5,
				SpringLayout.SOUTH, textFieldSchoolName);

		layout.putConstraint(SpringLayout.EAST, labelFieldSchoolStreet, -5,
				SpringLayout.WEST, textFieldSchoolStreet);
		layout.putConstraint(SpringLayout.NORTH, labelFieldSchoolStreet, 10,
				SpringLayout.SOUTH, textFieldSchoolName);

		layout.putConstraint(SpringLayout.WEST, textFieldSchoolNumber, 100,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, textFieldSchoolNumber, 5,
				SpringLayout.SOUTH, textFieldSchoolStreet);

		layout.putConstraint(SpringLayout.EAST, labelFieldSchoolNumber, -5,
				SpringLayout.WEST, textFieldSchoolTown);
		layout.putConstraint(SpringLayout.NORTH, labelFieldSchoolNumber, 10,
				SpringLayout.SOUTH, textFieldSchoolStreet);

		layout.putConstraint(SpringLayout.WEST, textFieldSchoolTown, 100,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, textFieldSchoolTown, 5,
				SpringLayout.SOUTH, textFieldSchoolNumber);

		layout.putConstraint(SpringLayout.EAST, labelFieldSchoolTown, -5,
				SpringLayout.WEST, textFieldSchoolTown);
		layout.putConstraint(SpringLayout.NORTH, labelFieldSchoolTown, 10,
				SpringLayout.SOUTH, textFieldSchoolNumber);

		layout.putConstraint(SpringLayout.WEST, textFieldSchoolDirector, 100,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, textFieldSchoolDirector, 5,
				SpringLayout.SOUTH, textFieldSchoolTown);

		layout.putConstraint(SpringLayout.EAST, labelFieldSchoolDirector, -5,
				SpringLayout.WEST, textFieldSchoolDirector);
		layout.putConstraint(SpringLayout.NORTH, labelFieldSchoolDirector, 10,
				SpringLayout.SOUTH, textFieldSchoolTown);

		layout.putConstraint(SpringLayout.WEST, textFieldSchoolContabil, 100,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, textFieldSchoolContabil, 5,
				SpringLayout.SOUTH, textFieldSchoolDirector);

		layout.putConstraint(SpringLayout.EAST, labelFieldSchoolContabil, -5,
				SpringLayout.WEST, textFieldSchoolContabil);
		layout.putConstraint(SpringLayout.NORTH, labelFieldSchoolContabil, 10,
				SpringLayout.SOUTH, textFieldSchoolDirector);

		layout.putConstraint(SpringLayout.WEST, modifySchoolButton, 170,
				SpringLayout.WEST, panel);
		layout.putConstraint(SpringLayout.NORTH, modifySchoolButton, 5,
				SpringLayout.SOUTH, textFieldSchoolContabil);

		layout.putConstraint(SpringLayout.WEST, cancelSchoolButton, 10,
				SpringLayout.EAST, modifySchoolButton);
		layout.putConstraint(SpringLayout.NORTH, cancelSchoolButton, 5,
				SpringLayout.SOUTH, textFieldSchoolContabil);

		frame.getContentPane().add(panel);
		frame.setVisible(true);
		frame.setSize(new Dimension(500, 350));
		frame.setPreferredSize(new Dimension(500, 350));

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int horizontalPosition, VerticalPosition;
		horizontalPosition = (int) d.getWidth() / 2
				- (int) frame.getSize().getWidth() / 2;
		VerticalPosition = (int) d.getHeight() / 2
				- (int) frame.getSize().getHeight() / 2;
		frame.setLocation(horizontalPosition, VerticalPosition);
	}

	private boolean isText(String text) {
		if (text == null)
			return false;
		return true;
	}

	public void setTextFieldSchoolContabil(JTextField textFieldSchoolContabil) {
		this.textFieldSchoolContabil = textFieldSchoolContabil;
	}

	public void setTextFieldSchoolDirector(JTextField textFieldSchoolDirector) {
		this.textFieldSchoolDirector = textFieldSchoolDirector;
	}

	public void setTextFieldSchoolName(JTextField textFieldSchoolName) {
		this.textFieldSchoolName = textFieldSchoolName;
	}

	public void setTextFieldSchoolNumber(JTextField textFieldSchoolNumber) {
		this.textFieldSchoolNumber = textFieldSchoolNumber;
	}

	public void setTextFieldSchoolStreet(JTextField textFieldSchoolStreet) {
		this.textFieldSchoolStreet = textFieldSchoolStreet;
	}

	public void setTextFieldSchoolTown(JTextField textFieldSchoolTown) {
		this.textFieldSchoolTown = textFieldSchoolTown;
	}

	public void dispose() {
		frame.dispose();

	}

}
